<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="标识" width="100" sortable>
      </el-table-column>
      <el-table-column prop="businessName" label="商机名" width="150" sortable>
      </el-table-column>
      <el-table-column prop="productName" label="产品名" width="150" sortable>
      </el-table-column>
      <el-table-column prop="customerName" label="客户名" width="150" sortable>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="80" sortable>
      </el-table-column>
      <!--0未分配  1跟进中 2已转化为商机 -1放入线索池-->
      <el-table-column prop="state" label="状态" width="180" sortable>
        <template scope="scope">
          <span v-if="scope.row.state=='0'" style="color: blue">待付款</span>
          <span v-if="scope.row.state=='1'" style="color: blue">已付订金</span>
          <span v-if="scope.row.state=='2'" style="color: orange">已完成</span>
          <span v-if="scope.row.state=='-1'" style="color: red">取消</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="250">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" @click="handlePay(scope.$index, scope.row)">缴费</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增或编辑界面
    :visible.sync="outerVisible" 新版本打开对话框
    v-model="orderFormVisible" 老版本打开对话框
    -->
    <el-dialog title="新增或修改" :visible.sync="orderFormVisible" :close-on-click-modal="false">
      <el-form :model="order" label-width="80px" :rules="orderFormRules" ref="orderForm">
        <el-form-item label="键" prop="key">
          <el-input v-model="order.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="order.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input type="textarea" v-model="order.intro"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="orderFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="orderLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="缴费" :visible.sync="payOrderVisible" :close-on-click-modal="false">
      <el-form :model="payOrderData" label-width="80px"  ref="payOrderForm">
        <el-form-item label="定单标识">
          <el-input  v-model="payOrderData.orderSn" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="应付金额(未扣定金)">
          <el-input  v-model="payOrderData.price"  disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品名">
          <el-input  v-model="payOrderData.productName" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="顾客名">
          <el-input  v-model="payOrderData.customerName" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="manager">
          <el-select v-model="payOrderData.payModel" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
               必须要写  value-key="id"
            -->
            <el-option
                v-for="item in payModelList"
                :key="item.id"
                :label="item.title"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="payOrderVisible = false">取消</el-button>
        <el-button type="primary" @click.native="payOrderSubmit">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //分页相关
      query:{
        currentPage:1,
        pageSize:10,
        keyword:''
      },
      pageInfo:{
        total:0,
        rows:[]
      },
      listLoading: false,
      sels: [],//列表选中列

      orderFormVisible: false,//新增界面是否显示
      payOrderVisible: false,//新增界面是否显示
      orderLoading: false,
      orderFormRules: {
        key: [
          { required: true, message: '请输入键', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '请输入值', trigger: 'blur' }
        ]
      },
      //新增界面数据
      order: {
        id:null,
        key: '',
        value: '',
        intro: '',
      },
      payModelList:[],
      payOrderData:{
        orderId:null,
        orderSn:'',
        price: 0,
        payModel:{},
        customerName:'',
        productName:''
      }

    }
  },
  methods: {
    getPayMethods(){
      //加载活动信息
      this.$http.get("/dictionaryitem/items/payModel")
          .then(result =>{
            result = result.data; //{success,result:{}}
            if (result.success){
              this.payModelList = result.data;
            }else{
              this.$message({
                message: '获取活动数据失败！'+result.message,
                type: 'error'
              });
            }
          })
          .catch(result=>{
            this.$message({
              message: '系统错误！',
              type: 'error'
            });
          })
    },
    handlePay(index,row){
      this.payOrderData={
        orderId:row.id,
        orderSn:row.sn,
        price: row.price,
        payModel:null,
        customerName:row.customerName,
        productName:row.productName
      }
      //打开缴纳定金对话框
      this.payOrderVisible = true;

      //赋值商机id
      this.payOrderData.orderId = row.id;

      //查询所有活动
      this.getPayMethods();

    },
    payOrderSubmit(){
      //发起请求获取数据
      this.$http.post("/order/pay",this.payOrderData)
          .then(result =>{
            result = result.data; //{success,result:{}}
            if (result.success){
              this.$message({
                message: '缴纳成功！',
                type: 'success'
              });

              this.payOrderVisible = false; //关闭对话框

              this.getOrders(); //刷新表格
            }else{
              this.$message({
                message: '缴费失败！'+result.message,
                type: 'error'
              });
            }
          })
          .catch(result=>{
            this.$message({
              message: '系统错误！',
              type: 'error'
            });
          })
    },
    search(){
      //重置当前页为第一页
      this.query.currentPage =1;
      this.getOrders();
    },
    handleCurrentChange(val) {
      this.query.currentPage= val;
      this.getOrders();
    },
    //获取用户列表
    getOrders() {
      this.listLoading = true; //发起请求之前显示加载条
      //发起请求获取数据
      this.$http.post("/order",this.query)
          .then(result =>{
            result = result.data; //{success,result:{}}
            if (result.success){
              this.pageInfo = result.data;
            }else{
              this.$message({
                message: '获取分页数据失败！'+result.message,
                type: 'error'
              });
            }
            this.listLoading = false;
          })
          .catch(result=>{
            this.$message({
              message: '系统错误！',
              type: 'error'
            });
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        removeOrder(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getOrders();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.orderFormVisible = true;
      this.order = Object.assign({}, row); //回显

    },
    //显示新增界面
    handleAdd: function () {
      this.orderFormVisible = true;
      this.order =   {
        id:null,
        key: '',
        value: '',
        intro: '',
      }
    },
    //编辑
    save: function () {
      this.$refs.orderForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.orderLoading = true;
            let para = Object.assign({}, this.order);
            //把数据转换成我们想要格式 parent: [1, 4]---》parent:{id:4}

            /*if(para.parent){// [2,7]
              para.parent = {"id":para.parent[para.parent.length-1]}
            }*/
            if (para.parent){
              let parentIds = para.parent; // [1, 4]
              let parentId = parentIds[parentIds.length-1]; // 4
              para.parent = {"id":parentId} //parent:{id:4}
            }

            this.$http.put("/order",para).then((res) => {
              res = res.data;

              if (res.success){
                this.$message({
                  message: '保存成功',
                  type: 'success'
                });

                this.orderFormVisible = false; //关闭对话框

                this.$refs['orderForm'].resetFields(); //重置表单

                this.getOrders(); //重新加载数据
              }else{
                this.$message({
                  message: '保存失败,'+res.message,
                  type: 'error'
                });
              }

              this.orderLoading = false; //关闭加载条
            });
          });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        batchRemoveOrder(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getOrders();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getOrders();
  }
}

</script>

<style scoped>

</style>
